
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

//取款界面
public class Save extends JPanel implements ActionListener {
    private Icon backImage = Util.getIcon("/image/pane.jpg");//返回按钮图
    private Icon saveImage = Util.getIcon("/image/buttonSave.jpg");//取款按钮图
    private ATM frame;//顶级容器实例
    private List<User> usersList;//用户信息集合实例
    private JButton backBtn;//返回按钮
    private JLabel jLabelBack;//返回文本
    private JLabel saveJLabel;//取款提示插件
    private JTextField iptSaveMoney;//取款输入框
    private JButton saveBtn;//取款按钮

    public Save(ATM frame) {
        this.frame = frame;
        //设置空布局和透明度
        this.setLayout(null);
        this.setOpaque(false);
        //取款按钮
        saveBtn = new JButton(saveImage);
        saveBtn.setBounds(870, 220, 140, 50);
        this.add(saveBtn);
        saveBtn.addActionListener(this);
        //取款提示插件
        saveJLabel = new JLabel("输入取款金额：");
        saveJLabel.setBounds(230, 220, 400, 50);
        saveJLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        this.add(saveJLabel);
        //取款输入框
        iptSaveMoney = new JTextField();
        iptSaveMoney.setBounds(420, 220, 300, 50);
        iptSaveMoney.setFont(new Font("宋体", Font.BOLD, 35));
        this.add(iptSaveMoney);

        //返回按钮
        jLabelBack = new JLabel("返  回");
        jLabelBack.setBounds(900, 300, 100, 50);
        jLabelBack.setForeground(Color.white);
        this.add(jLabelBack);
        jLabelBack.setFont(new Font("宋体", Font.BOLD, 25));
        backBtn = new JButton(backImage);
        backBtn.setBounds(870, 300, 140, 50);
        backBtn.setOpaque(false);
        this.add(backBtn);
        backBtn.addActionListener(this);//建立联系
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == backBtn) {
            frame.switchPanel("menu");
            iptSaveMoney.setText("");
        } else if (e.getSource() == saveBtn) {
            double money = Double.parseDouble(iptSaveMoney.getText());
            double balance = frame.getLoginUser().getBalance();//余额
            if (money > balance) {
                JOptionPane.showMessageDialog(this, "余额不足，请重新输入");
                return;
            }
            if (money % 100 != 0) {
                JOptionPane.showMessageDialog(this, "请输入100的倍数");
                return;
            }
            frame.getLoginUser().setBalance(balance - money);
            JOptionPane.showMessageDialog(this, "取款成功，已取款：" + money);
            String sql = "UPDATE t_user SET u_balance =" + frame.getLoginUser().getBalance() +
                    " WHERE u_account = '" + frame.getLoginUser().getAccount() + "'";
            System.out.println(sql);
          Util.insert(sql);
            iptSaveMoney.setText("");
        }
    }
}
